package com.alan.controller;

import com.alan.service.RegisterService;
import com.alan.service.impl.RegisterServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    private RegisterService registerService = new RegisterServiceImpl();

    /**
     * 处理注册的业务逻辑
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String tclass = req.getParameter("tclass");
        String identify = req.getParameter("identify");
        String re = registerService.add(username, password, tclass, identify);
        if (re == "success") {
            req.getRequestDispatcher("success.jsp?info=regsuccess").forward(req, resp);
        } else {
            req.setAttribute("info","haved");
            req.getRequestDispatcher("fail.jsp").forward(req,resp);
        }
    }
}
